package weaver.interfaces.unine;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName FixedAssetsAction
 * @Description TODO
 * @Author ocean
 * @Date 2022/8/29 16:47
 * @Version 1.0
 */
public class SupplierUpdateAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        SupplierUtil supplierUtil = new SupplierUtil();
        String requestid = requestInfo.getRequestid();
        int formId = requestInfo.getRequestManager().getFormid();
        writeLog(">>>>>>SupplierUpdateAction>>>>>" + requestid);
        try {
            Map mainTableDataMap = new HashMap();
            //取主表数据
            // 获取表单主字段信息
            Property[] properties = requestInfo.getMainTableInfo().getProperty();
            for (Property property : properties) {
                // 主字段名称
                String name = property.getName().toLowerCase();
                // 主字段对应的值
                String value = Util.null2String(property.getValue());
                mainTableDataMap.put(name, value);
            }
            String gfszzgs = Util.null2String(mainTableDataMap.get("gfszzgs"));
            String ygfmc = supplierUtil.getu9gysbm(Util.null2String(mainTableDataMap.get("ygfmc")),"u9gysbm");
            String ghdm = supplierUtil.getu9gysbm(Util.null2String(mainTableDataMap.get("ygfmc")),"ghdm");
            String gyslx = supplierUtil.getSelectValue(formId, "gyslx", supplierUtil.getu9gysbm(Util.null2String(mainTableDataMap.get("ygfmc")), "gyslx"));
            String sfwlsgys1 = supplierUtil.getSelectValue(formId, "sfwlsgys1", supplierUtil.getu9gysbm(Util.null2String(mainTableDataMap.get("ygfmc")), "sfwlsgys1"));
            String orgCode = "999";
            String bghmc = Util.null2String(mainTableDataMap.get("bghmc"));
            String xgysyxqc = Util.null2String(mainTableDataMap.get("xgysyxqc"));
            String xgyszhkh = Util.null2String(mainTableDataMap.get("xgyszhkh"));
            String gysdz1 = Util.null2String(mainTableDataMap.get("gysdz1"));
            String xkplx1 = supplierUtil.getSelectValue(formId, "xkplx1", Util.null2String(mainTableDataMap.get("xkplx1")));
            String jsfs1 = supplierUtil.getSelectValue(formId, "jsfs1", Util.null2String(mainTableDataMap.get("jsfs1")));
            String xsl = supplierUtil.getSelectValue(formId, "xsl", Util.null2String(mainTableDataMap.get("xsl")));
            String zq1 = supplierUtil.getSelectValue(formId, "zq1", Util.null2String(mainTableDataMap.get("zq1")));

            Double xslDouble = Double.parseDouble( xsl.replace( "%", "" ) ) * 0.01;

            String action = "IUpdSupplySV.svc/do";
            String url = getPropValue("u9yk", "url") + action;


            JSONObject contextMap = supplierUtil.getContextInfo();
            Map<String, Object> inputMap = new HashMap<String, Object>();


            inputMap.put("orgCode", orgCode);
            inputMap.put("code", ygfmc);
            inputMap.put("name", bghmc);
            inputMap.put("bankName", xgysyxqc);
            inputMap.put("invType",xkplx1);
            inputMap.put("bankAccountName", xgyszhkh);
            inputMap.put("setMethod", jsfs1);
            inputMap.put("taxRate",xslDouble);
            inputMap.put("zcAddress", gysdz1);
            inputMap.put("suppType",gyslx);
            inputMap.put("isMISC", sfwlsgys1);
            inputMap.put("accPeriod", zq1);
            inputMap.put("confirmCode",ghdm);



            String back = supplierUtil.doSupplier(url, JSON.toJSONString(inputMap));
            writeLog("JSON.toJSONString(inputMap)=" + JSON.toJSONString(inputMap));
            Map<String, Object> jsonMap = JSON.parseObject(back, HashMap.class);
            Map<String, Object> backJson = JSON.parseObject(Util.null2String(jsonMap.get("d")), HashMap.class);
            writeLog("backJson=" + backJson);
            String status = Util.null2String(backJson.get("Status"));
            writeLog("status=" + status);
            String msg = Util.null2String(backJson.get("Msg"));
            writeLog("msg=" + msg);
            if ("1".equals(status)) {
                String code = Util.null2String(backJson.get("Code"));
                writeLog("code=" + code);
                return SUCCESS;
            } else {
                requestInfo.getRequestManager().setMessageid("1111112");
                requestInfo.getRequestManager().setMessagecontent(msg);
                return FAILURE_AND_CONTINUE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}